local MobaTopView = MClass("MobaTopView")
local ColorWarnning = cc.c3b(242, 0, 0)
local ColorNormal = cc.c3b(242, 253, 135)

function MobaTopView:ctor()
    --self.root 默认为根节点
    --设置事件
    self.escButton = self.root:findChild("btn_esc")
    assert(self.escButton)
    self.setButton = self.root:findChild("btn_set")
    assert(self.setButton)
    self.rankButton = self.root:findChild("btn_rank")
    assert(self.rankButton)
    self.mapButton = self.root:findChild("btn_map")
    assert(self.mapButton)

    --time label
    self.timeLabel = self.root:findChild("clock/time")
    assert(self.timeLabel)

    self.leftScore = self.root:findChild("num_1")
    self.rightScore = self.root:findChild("num_2")

    self.killTimes = self.root:findChild("sum/kill")
    self.deadTimes = self.root:findChild("sum/die")
    self.assistTimes = self.root:findChild("sum/assist")

    self:init()
end

function MobaTopView:init()
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_TIME,handler(self,MobaTopView.setTime))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.MELEE_SCORE,handler(self,MobaTopView.setScore))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.MELEE_STATUS,handler(self,MobaTopView.setStatus))

    

    --逃跑
    local function touchEvent_Esc(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            local function okFunc()
                BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_ESCAPE)
            end
            app:popMessage("escape", nil, {ok = okFunc}, Enums.ToastTag.battle)
        end
    end
    self.escButton:addTouchEventListener(touchEvent_Esc)

    --设置
    local function touchEvent_setting(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            UIHandler:handleMessage({id=Constant.FUNC_TYPE_SET, mode="open", data=2})
        end
    end
    self.setButton:addTouchEventListener(touchEvent_setting)


     --小地图
    local function touchEvent_map(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
                BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SHOW_MELEE_MAP)
        end
    end
    self.mapButton:addTouchEventListener(touchEvent_map)

     --统计数据
    local function touchEvent_rank(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            print("touchEvent_rank")
            --BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SHOW_MELEE_RANK)
        end
    end
    self.rankButton:addTouchEventListener(touchEvent_rank)

end

function MobaTopView:setStatus(killTimes,assistTimes,deadTimes)
    -- body
    self.killTimes:setString(killTimes)
    self.assistTimes:setString(assistTimes)
    self.deadTimes:setString(deadTimes)
end

function MobaTopView:setScore(left,right)
    self.leftScore:setString(left or 0)
    self.rightScore:setString(right or 0)
end

function MobaTopView:setTime(time)
        time = tonumber(time)
        self.second = math.floor(time/1000) -- 总秒
        self.min = math.floor(self.second/60) -- 分
        self.second = math.floor(self.second%60) -- 分
        self.timeLabel:setString(string.format("%02d:%02d",self.min,self.second))
        if time < 60000 then
            self.timeLabel:setColor(ColorWarnning)
        else
            self.timeLabel:setColor(ColorNormal)
        end 
end

return MobaTopView